package com.jqsoft.launcher.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;

import com.jqsoft.launcher.util.XmlUtil;

public class CeshiFollowUpActivity extends WebViewActivity implements
		OnClickListener {

	@Override
	protected void initGui() {
		super.initGui();
		webView.loadUrl("file:///android_asset/test_followup_record.html");
		webView.addJavascriptInterface(new JSObject(), "contact");
	}

	@Override
	protected void initAction() {
		setGoBack();
	}

	@Override
	protected void initData() {
		setTitle("测试分析数据表");
	}

	@Override
	public void onClick(View v) {
	}

	private final class JSObject {
		//调用蓝牙接口返回xml数据
		@JavascriptInterface
		public void getXML(String type){
			Intent intent = new Intent();
			// 通讯接口
			intent.setComponent(new ComponentName("com.example.uray.bluetooth.comm.app", "com.example.uray.bluetooth.comm.app.CommActivity"));
			// 参数配置接口
			//intent.setComponent(new ComponentName("com.example.uray.bluetooth.comm.app", "com.example.uray.bluetooth.comm.app.ConfigActivity"));
			intent.setAction(Intent.ACTION_VIEW);   
			if(type.equals("1")){ 
				startActivityForResult(intent, 1);  //获取尿液数据
			}else if(type.equals("2")){
				startActivityForResult(intent, 2);  //获取监护数据
			}else if(type.equals("3")){
				startActivityForResult(intent, 3);  //获取血糖数据
			}else if(type.equals("4")){
				startActivityForResult(intent, 4);  //获取血红蛋白数据
			}
		}
	}
	
	// 解析蓝牙接口返回的XML格式的字符串
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
          super.onActivityResult(requestCode, resultCode, data); 
        if(requestCode == 1 && resultCode == RESULT_OK){
        	//获取尿液数据
        	String sb = data.getStringExtra("return");
        	String jsonStr =  XmlUtil.parseXml(sb);
        	webView.loadUrl("javascript:showXML('" + jsonStr + "')");
        } 
        if(requestCode == 2 && resultCode == RESULT_OK){
        	//获取监护数据
        	String sb = data.getStringExtra("return");
        	String jsonStr =  XmlUtil.parseXml(sb);
        	webView.loadUrl("javascript:showXML2('" + jsonStr + "')");
        } 
        if(requestCode == 3 && resultCode == RESULT_OK){
        	//获取血糖数据
        	String sb = data.getStringExtra("return");
        	String jsonStr =  XmlUtil.parseXml(sb);
        	webView.loadUrl("javascript:showXML3('" + jsonStr + "')");
        }
        if(requestCode == 4 && resultCode == RESULT_OK){
        	//获取血红蛋白数据
        	String sb = data.getStringExtra("return");
        	String jsonStr =  XmlUtil.parseXml(sb);
        	webView.loadUrl("javascript:showXML4('" + jsonStr + "')");
        }
    }
}
